﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using Qianwei.Model;
using Qianwei.Biz;
using Qianwei.Common.Extension;
using Qianwei.Common;

namespace Qianwei.Web.Admins
{
    public partial class AddCompanyNews : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["nid"] != null)
            {
                BindData();
            }
        }

        private void BindData()
        {
            int nid = Request["nid"].TryInt();
            news_center news = NewsCenterManager.Instance.GetModel(nid);
            txtNew_Title.Text = news.New_Title;
            hf_contentinit.Value = news.New_Content;
        }

        private string CheckInput()
        {
            int nid = Request["nid"].TryInt();
            string strErr = string.Empty;
            if (txtNew_Title.Text.Trim().Length == 0)
            {
                strErr = "请输入标题";
            }
            //else if (txtNew_Content.Text.Trim().Length == 0)
            //{
            //    strErr = "请输入内容";
            //}
            else if (hfNew_Content.Value.Trim().Length == 0)
            {
                strErr = "请输入内容";
            }
            return strErr;
        }

        private void SaveJob()
        {
            int nid = Request["nid"].TryInt();
            news_center news = new news_center();
            news.Create_Time = DateTime.Now;
            news.Last_Change_Time = news.Create_Time;
            news.New_Author = HttpContext.Current.User.Identity.IsAuthenticated ? HttpContext.Current.User.Identity.Name :　"unknow";
            news.New_Content = hfNew_Content.Value;
            news.New_Title = txtNew_Title.Text.Trim();
            news.New_Type = (int)AppEnum.NewType.CompanyNew;
            news.New_Center_Id = nid;

            if (nid == 0)
            {
                if (NewsCenterManager.Instance.Add(news) > 0)
                {
                    //BindData();
                    hf_contentinit.Value = hfNew_Content.Value;
                    Util.PageAlert("保存信息成功");
                }
                else
                {
                    Util.PageAlert("对不起,保存信息失败,请稍后重试");
                }
            }
            else
            {
                if (NewsCenterManager.Instance.Update(news))
                {
                    BindData();
                    Util.PageAlert("更新信息成功");
                }
                else
                {
                    Util.PageAlert("对不起,更新信息失败,请稍后重试");
                }
            }
        }
        #region Event

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string result = CheckInput();
            if (result.Length > 0)
            {
                Util.PageAlert(result);
            }
            else
            {
                SaveJob();
            }
        }
        #endregion
    }
}